package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.AssessmentPersonMapper;
import com.zhijian.medical.entity.po.AssessmentPerson;
import com.zhijian.medical.entity.vo.request.AssessmentPersonSearchVo;
import com.zhijian.medical.entity.vo.response.AssessmentPersonVo;
import com.zhijian.medical.service.AssessmentPersonService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 考核-人员关系表，目前只有专项培训人员需要业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-12-02 15:22:11
 */
@Slf4j
@Service
public class AssessmentPersonServiceImpl implements AssessmentPersonService {

    @Autowired
    private AssessmentPersonMapper mapper;

    @Override
    public AssessmentPerson selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public AssessmentPersonVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(AssessmentPerson assessmentPerson) {
        Date now = new Date();
        assessmentPerson.setCreateTime(now);
        assessmentPerson.setUpdateTime(now);
        return mapper.insert(assessmentPerson) > 0 ? assessmentPerson.getId() : 0L;
    }

    @Override
    public boolean update(AssessmentPerson assessmentPerson) {
        Date now = new Date();
        assessmentPerson.setUpdateTime(now);
        return mapper.updateByPrimaryKey(assessmentPerson) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(AssessmentPerson record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(AssessmentPersonSearchVo assessmentPersonSearchVo) {
        return mapper.count(assessmentPersonSearchVo);
    }

    @Override
    public List<AssessmentPersonVo> selectList(AssessmentPersonSearchVo assessmentPersonSearchVo) {
        return mapper.selectList(assessmentPersonSearchVo);
    }

    @Override
    public Page<AssessmentPersonVo> selectPage(AssessmentPersonSearchVo assessmentPersonSearchVo) {
        //先直接查总数
        int count = count(assessmentPersonSearchVo);
        //分页
        Page<AssessmentPersonVo> page = new Page<>(true, count, assessmentPersonSearchVo.getPageNum(), assessmentPersonSearchVo.getPageSize());
        assessmentPersonSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<AssessmentPersonVo> responseVos = this.selectList(assessmentPersonSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<AssessmentPerson> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

}